//
//  HRChoosePreferentialViewController.m
//  haierapp
//
//  Created by Xia Zhiyong on 14-8-14.
//  Copyright (c) 2014年 haier. All rights reserved.
//

#import "HRChoosePreferentialViewController.h"
#import "HRChoosePreferentialTableViewCell.h"
#import "HRCouponInfoModel.h"
#import "HRCouponListModel.h"

@interface HRChoosePreferentialViewController (){
    NSInteger currentSelect;     //默认－1.未选择优惠券
    NSInteger beforeSelect;
    
    NSMutableArray *arrayTable;
}

@end

@implementation HRChoosePreferentialViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        arrayTable = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    if (__CUR_IOS_VERSION >= __IPHONE_7_0)
    {
        self.edgesForExtendedLayout= UIRectEdgeNone;
    }
    self.navigationItem.title = @"选择优惠券";
    currentSelect = -1;
    beforeSelect = -1;
    [self loadData];
    
}

-(void)loadData{
    [self showCustomLoadingHud:kLoading inView:nil];
    [HRInterfaceManager getCouponList:^(BOOL isSucceed, NSString *message, id data) {
        
        [self hideCustomLoadingHud];
        
        if (isSucceed) {
            HRCouponListModel *list = (HRCouponListModel*)data;
            NSArray *array = [NSArray arrayWithArray:list.list];
            for (HRCouponInfoModel *info in array) {
                if ([info.isUsed isEqualToString:@"0"] &&[info.isGive isEqualToString:@"1"] &&[info.isOverdueDate isEqualToString:@"0"]) {
                    [arrayTable addObject:info];
                }
            }
            if (arrayTable.count) {
                [self hideHud];
                _viewBottom.hidden = NO;
                [_tableView reloadData];
                
            }else{
                [self showTextHUD:@"没有可用优惠券"];
                [self showHudByType:HUDTYPE_COUPON];
                _viewBottom.hidden = YES;
            }
            
        }else{
            [self showHudByType:HUDTYPE_COUPON];
            _viewBottom.hidden = YES;
        }
    }];
    
}

#pragma mark - UITableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return arrayTable.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 45;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"HRChoosePreferentialTableViewCell";
    HRChoosePreferentialTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[HRChoosePreferentialTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    }
    
    if (currentSelect == indexPath.row) {
        cell.btnSelect.selected = YES;
    }else{
        cell.btnSelect.selected = NO;
    }
    HRCouponInfoModel *info = [arrayTable objectAtIndex:indexPath.row];
    cell.lblText.text = [NSString stringWithFormat:@"<font  size=12 color='#303030'>%@ </font><font  size=10 color='#606060'>订单满%@元立减%@元</font>",info.amount,info.serviceRestrictions,info.amount];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    beforeSelect=currentSelect;
    currentSelect = indexPath.row;
    
    //点击两次,取消购物券
    if (beforeSelect==currentSelect) {
        currentSelect=-1;//未选中
        beforeSelect=-1;
    }
    
    
    [tableView reloadData];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 确定
- (IBAction)actionSure:(id)sender {
  
    if (_choosePreferential && currentSelect!=-1) {
        
        HRCouponInfoModel *info = [arrayTable objectAtIndex:currentSelect];
        //判断是否满足优惠券使用条件
        if ([self.priceStr floatValue]<[info.serviceRestrictions floatValue]) {
            [self showTextHUD:@"不满足优惠券使用条件"];
            return;
        }
        
        _choosePreferential(info.amount,info.couponId);
    }else if (_choosePreferential){
        //没有选择优惠券
        _choosePreferential(@"0",@"");
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - HRProgress Delegate Methods
//马上去找
-(void)tapHUDButtonAction:(id)button{
    //回到首页
    [self.navigationController popViewControllerAnimated:YES];
}

@end
